package com.imessage.action.manage.menu;

import com.bean.ManageResource;
import com.imessage.comm.Constants;
import com.imessage.type.ManageResourceType;
import com.imessage.vo.MenuVo;
import com.imessage.web.HttpJson;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/menu")
public class MenuAction extends MenuBaseAction{

	@RequestMapping(value = "")
	public Object run(MenuVo menuVo) {
		List<Expression> expressionList=new ArrayList<Expression>();
		setExpressionList(expressionList, menuVo);
		Page<ManageResource> page=baseService.findPage(ManageResource.class, expressionList,
				null, getOrder(menuVo.getOrders()), menuVo.getPageCurrent(),
				menuVo.getPageSize());
		return page;
	}

	@RequestMapping(value = "initpage")
	public ModelAndView initpage(
			@RequestParam(name="req_id",required = true)String id){
		ModelAndView modelAndView = new ModelAndView();
        ManageResource manageMenu=new ManageResource();
		if(StringUtils.isNotBlank(id)){
			manageMenu=baseService.findById(ManageResource.class, id);
			modelAndView.setViewName("admin/manage/menu/modify");
		}else{
			modelAndView.setViewName("admin/manage/menu/add");
		}
		setModelAndView(modelAndView,manageMenu);
		return modelAndView;
	}



	@RequestMapping(value = "add")
	public Object add(MenuVo menuVo) {
		System.out.println("添加菜单："+menuVo.toString());
        ManageResource manageMenu=new ManageResource();
		setManageMenu(menuVo, manageMenu);
		if(manageMenu.getManageHighResourceId()!=null&&manageMenu.getManageHighResourceId()>0){
            manageMenu.setType(ManageResourceType.menu);
        }else{
            manageMenu.setType(ManageResourceType.path);
        }
		int state=baseService.saveObject(manageMenu);
		if(state==Constants.STATE_OPERATOR_SUCC){
			return getJsonForSucc("", datagrid);
		}
		return getJsonForErr("", datagrid);
	}


	@RequestMapping(value = "modify")
	public Object modify(MenuVo menuVo) {
        ManageResource manageMenu=baseService.findById(ManageResource.class, menuVo.getId());
		setManageMenu(menuVo, manageMenu);
		int state=baseService.updateObject(manageMenu);
		if(state==Constants.STATE_OPERATOR_SUCC){
			return manageMenu;
		}
		return getJsonForErr("", datagrid);
	}
	@RequestMapping(value = "del")
	public Object del(@RequestParam(name="id",required = true)String ids) {
		int state=Constants.STATE_OPERATOR_LOST;
		for(String id:ids.split(",")){
            ManageResource manageMenu=baseService.findById(ManageResource.class,id);
			state=baseService.delObject(manageMenu);
		}
		if(state==Constants.STATE_OPERATOR_SUCC){
			return getJsonForSucc("", datagrid);
		}
		return getJsonForErr("", datagrid);
	}


}
